# -*- coding: utf-8 -*-
#loading levels and stuff

import pygame

import data

#hårdkodar lite leveldata
level_width = 1000
level_height = 1000

class BasicLevel:
    def __init__(self, file):
        """Set up and load a tile based level"""
        self.bggrid_size = (20, 20)
        self.fggrid_size = (20, 20)
        #self.bggrid = [[0, 0, 0, 0, 0, 0]]
        self.bggrid = [[0, 0, 0, 0, 0, 1, 3, 3, 2, 0, 0, 1, 0, 1, 0, 0, 2, 3, 1, 1],
                       [3, 3, 2, 2, 0, 2, 1, 0, 0, 1, 1, 0, 0, 1, 3, 1, 1, 0, 2, 3],
                       [1, 0, 1, 1, 3, 3, 1, 3, 2, 0, 1, 3, 0, 2, 1, 1, 3, 0, 0, 0],
                       [0, 2, 2, 0, 1, 1, 2, 3, 3, 0, 2, 2, 1, 2, 0, 1, 0, 1, 1, 3],
                       [1, 1, 2, 0, 3, 1, 3, 3, 2, 0, 1, 3, 3, 0, 2, 2, 3, 0, 2, 3],
                       [2, 0, 3, 0, 0, 0, 3, 0, 0, 3, 1, 1, 0, 1, 0, 2, 1, 2, 0, 0],
                       [0, 2, 1, 0, 0, 3, 0, 2, 2, 0, 0, 2, 3, 3, 0, 0, 3, 1, 3, 0],
                       [0, 2, 2, 2, 0, 3, 3, 2, 3, 2, 1, 1, 0, 0, 2, 0, 3, 3, 3, 2],
                       [0, 2, 0, 3, 1, 2, 0, 1, 0, 0, 2, 1, 3, 2, 1, 0, 0, 3, 3, 1],
                       [3, 1, 1, 2, 2, 1, 0, 1, 1, 0, 2, 0, 1, 1, 0, 1, 0, 0, 0, 0],
                       [3, 3, 1, 3, 2, 2, 0, 3, 0, 1, 2, 3, 1, 3, 1, 1, 2, 1, 0, 0],
                       [1, 3, 3, 1, 3, 3, 3, 1, 3, 0, 2, 1, 1, 0, 2, 3, 2, 0, 2, 2],
                       [3, 0, 1, 3, 2, 1, 0, 1, 3, 3, 2, 3, 3, 2, 1, 1, 1, 3, 0, 3],
                       [1, 0, 1, 2, 2, 1, 3, 2, 1, 0, 2, 0, 1, 2, 1, 0, 2, 3, 1, 1],
                       [0, 3, 1, 3, 0, 3, 3, 0, 1, 0, 0, 1, 2, 2, 3, 2, 0, 0, 0, 3],
                       [3, 1, 1, 0, 0, 2, 2, 2, 1, 3, 1, 2, 2, 0, 2, 1, 2, 1, 2, 2],
                       [3, 3, 3, 0, 3, 3, 2, 3, 0, 3, 0, 1, 3, 2, 0, 1, 3, 2, 3, 2],
                       [2, 2, 2, 2, 2, 2, 1, 3, 2, 3, 3, 3, 2, 3, 0, 3, 3, 0, 2, 1],
                       [2, 3, 2, 2, 3, 1, 3, 2, 2, 1, 2, 1, 2, 1, 0, 0, 2, 3, 0, 3],
                       [0, 1, 3, 0, 0, 3, 1, 0, 3, 0, 1, 3, 2, 1, 1, 1, 2, 0, 0, 2]]



        #[[0, 1, 0, 0, 0],
        #               [0, 0, 3, 0, 0],
        #               [0, 0, 0, 0, 0],
        #               [0, 0, 0, 2, 0]]
        #               
        self.fggrid = [[]]

        self.tile_w = 100
        self.tile_h = 100
        
        #The parallaxing background needs to be bigger...but how much?
        self.bg_scale = 2
        self.fg_scale = 1

        self.player_start = (0,0)

        #We only store the tiles once, and reference them by a number
        self.tilemap, self.tile_rect = data.load_image("tiles.png")
        self.tilemap_dimensions = (self.tile_rect.width / self.tile_w,
                                   self.tile_rect.height / self.tile_h)
        
    def draw(self, camera, surface):
        """Draw the part of level that can be seen by camera,
        to surface"""
        self.draw_bg(camera, surface)
        #self.draw_fg(camera, surface)

    def get_tile(self, index):
        """Return a Rect surrounding the tile in tilemap, pointed
        to by index"""
        w = self.tile_w
        h = self.tile_h
        
        x = index % self.tilemap_dimensions[0] * w
        y = index // self.tilemap_dimensions[1] * h
        
        return pygame.Rect(x, y, w, h)

    def draw_bg(self, camera, surface):
        """Draw the topmost layer of the parallaxing background"""
        start_tile_x = (((camera.x / self.bg_scale)
                         / self.tile_w)
                        % self.bggrid_size[0])
        start_tile_y = -(((camera.y / self.bg_scale)
                         / self.tile_h)
                        % self.bggrid_size[1])
        start_draw_x = -((camera.x / self.bg_scale)
                         % self.tile_w)
        start_draw_y = -((camera.y / self.bg_scale)
                         % self.tile_h)

        y = start_tile_y 
        draw_y = start_draw_y
        while draw_y < surface.get_height():
            x = start_tile_x
            draw_x = start_draw_x
            while draw_x < surface.get_width():
                #Only draw defined tiles, or make them black
                if ((0 <= x and x < self.bggrid_size[0]) and
                    (0 <= y and y < self.bggrid_size[1])):
                        area = self.get_tile(self.bggrid[y][x])
                        dest = (draw_x, draw_y)
                        surface.blit(self.tilemap, dest, area)
                else:
                    empty_rect = pygame.Surface((self.tile_w, self.tile_h))
                    empty_rect.fill((0,0,0))
                    surface.blit(empty_rect, (draw_x, draw_y))

                #draw_x %= self.bggrid_size[0]
                draw_x += self.tile_w 
                
                x += 1
                
            #draw_y %= self.bggrid_size[1]
            draw_y += self.tile_h 

            y += 1
        #for y in xrange(0, camera.height/self.tile_h):
        #    for x in xrange(0, camera.width/self.tile_w):
        #        area = self.get_tile(self.bggrid[y][x])#, self.bggrid_size)
        #        dest = (self.tile_w * x, self.tile_h * y)
        #        surface.blit(self.tilemap, dest, area)
    
    def draw_fg(self, camera, surface):
        """Draw the bottom layer of the parallaxing background"""
        start_tile_x = (((camera[0] / self.fg_scale)
                         / self.tile_w)
                        % self.fggrid_size[0])
        start_tile_y = (((camera[1] / self.fg_scale)
                         / self.tile_h)
                        % self.fggrid_size[1])

        draw_pos = (0,0)

        for y in xrange(0, camera.height/self.tile_h):
            for x in xrange(0, camera.width/self.tile_w):
                area = self.get_tile(self.fggrid[x][y], self.fggrid_size)
                dest = (self.tile_w * x, self.tile_h * y)
                surface.blit(self.tilemap, dest, area)

